package servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import security.Encryption;

import entities.User;

import managers.UserManager;

@WebServlet("/Register")
public class RegisterServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	@EJB
	UserManager userManager;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.getRequestDispatcher("register.tag").forward(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		User user = (User) request.getSession().getAttribute("user");
		List<String> errors = new ArrayList<String>();
		if(user != null){
			response.sendRedirect(request.getServletContext().getContextPath());
		}else{
			user = new User();
			String username = request.getParameter("username");
			String email = (String) request.getParameter("email"); 
			String emailconfirm = (String) request.getParameter("emailconfirm");
			String password = (String) request.getParameter("password");
			String passwordconfirm = (String) request.getParameter("passwordconfirm");
			String fname = (String) request.getParameter("fname");
			String lname = (String) request.getParameter("lname");
			
			boolean error = false;
			
			if(userManager.getUsers().size() < 1 || username.equalsIgnoreCase("admin")){
				user.setRole("admin");
			}else{
				user.setRole("user");
			}			
			if(username != null){
				user.setUsername(username);
			}else{
				error = true;
				errors.add("Enter a username");
			}
			if(email != null){
				if(emailconfirm != null){
					if(email.equals(emailconfirm)){
						user.setEmail(email);
					}else{
						error = true;
						errors.add("Emails don't match");
					}
				}else{
					error = true;
					errors.add("Enter confirm email");
				}
			}else{
				error = true;
				errors.add("Enter an email");
			}
			if(password != null){
				if(passwordconfirm != null){
					if(Encryption.digest(password).equals(Encryption.digest(passwordconfirm))){
						user.setPassword(Encryption.digest(password));
					}else{
						error = true;
						errors.add("Passwords don't match");
					}
				}else{
					error = true;
					errors.add("Enter confirm password");
				}				
			}else{
				error = true;
				errors.add("Enter a password");
			}
			if(fname != null){
				user.setFname(fname);
			}
			if(lname != null){
				user.setLname(lname);
			}
			if(user.getEmail() != null && user.getUsername() != null && user.getPassword() != null){
				for(User u : userManager.getUsers()){
					if(u.getEmail().equals(user.getEmail())){
						error = true;
						errors.add("Email already in use");
					}
					if(u.getUsername().equals(user.getUsername())){
						error = true;
						errors.add("Username alread taken");
					}
				}				
			}
			if(!error){					
				userManager.create(user);					
				request.getSession().setAttribute("user", user);
				request.getRequestDispatcher("/WEB-INF/Overview.jsp").forward(request, response);
			}else{
				request.setAttribute("errors", errors);
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
		}
	}
}
